package com.codez.unionpay.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 *  @author codez
 *	@createTime 2019-05-18 04:58:08
 */

public class NettyClient {
	
	public static void main(String[] args) throws Exception {
		String host = "127.0.0.1";
		Integer port = 8080;
		NioEventLoopGroup group = new NioEventLoopGroup(1); // 客户端是不是可以单线程
 		
		Bootstrap bootstrap = new Bootstrap();
		
		bootstrap.group(group)
				.channel(NioSocketChannel.class)
				.option(ChannelOption.SO_KEEPALIVE, true)
				.handler(new ChannelInitializer<SocketChannel>() {

					@Override
					protected void initChannel(SocketChannel ch) throws Exception {
						ch.pipeline().addLast(new NettyClientHandler());
					}

				});
		
	    // Start the client.
        ChannelFuture f = bootstrap.connect(host, port).sync(); // (5)

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
		
	}
	
}
